// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * @brief
 * @version 1.0
 * @date ~ 2022-12-30
 * @author ~ 宋炜
 */

/// ADDED 宋炜 添加命令行参数
#include <stdlib.h>

#include <getopt.h>

#if defined(WIN32)
#   include <windows.h>
#   include <shellapi.h>
#else
#   include <sys/stat.h>
#   include <errno.h>
#   include <locale.h>
#endif

// dynamic link Qt4 plugins.
#include <QPluginLoader>
void loadNecessaryQtPlugins()
{
	static QPluginLoader qkrcodecsLoader( "qkrcodecs" );
	static QPluginLoader qcncodecsLoader( "qcncodecs" );
	static QPluginLoader qjpcodecsLoader( "qjpcodecs" );
	static QPluginLoader qtwcodecsLoader( "qtwcodecs" );
	static QPluginLoader qjpegLoader("qjpeg");
	static QPluginLoader qgifLoader("qgif");
	static QPluginLoader qmngLoader("qmng");
   
	qkrcodecsLoader.load();
	qcncodecsLoader.load();
	qjpcodecsLoader.load();
	qtwcodecsLoader.load();
	qjpegLoader.load();
	qgifLoader.load();
	qmngLoader.load();
}

#include <QApplication>
#include <QFontDatabase>
#include <QTextCodec>

#include "fqterm_frame.h"
#include "fqterm_path.h"
#include "fqterm_trace.h"

struct stCtx
{
    std::string     m_addr;
    uint16_t        m_port;
};

stCtx   gCtx;

bool parseCmd( int argc , char * const argv[] )
{
    bool ret = true;
    int c = 0;
    while( ( c = getopt( argc , argv , "a:p:" )) != -1 ){
        switch( c ){
        case 'a':
            gCtx.m_addr.assign( optarg , strlen( optarg ) );
            break;
        case 'p':
            gCtx.m_port = (uint16_t)atoi( optarg );
            break;
        }
    }

    return ret;
}

int main(int argc, char **argv)
{
	QApplication a(argc, argv);
    a.setApplicationName("vTerminal");
    FQTerm::setMaxTraceLevel(3);

	using namespace FQTerm;

    //loadNecessaryQtPlugins();

	if (!iniSettings()) {
		return -1;
	}

	FQTermFrame *mw = new FQTermFrame();

    mw->setWindowTitle("vTerminal " + QString(FQTERM_VERSION_STRING));
	mw->setWindowIcon(QPixmap(getPath(RESOURCE) + "pic/fqterm.png"));
	mw->show();

    if( argc == 5 ){
        bool rst = parseCmd( argc , argv );
        if( rst == true ){
            mw->connectSvr( gCtx.m_addr , gCtx.m_port );
        }
    }

	return a.exec();
}
